package quickmall

import grails.converters.JSON
import groovy.json.JsonSlurper

import org.springframework.dao.DataIntegrityViolationException

class OfferController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


	
	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		if(!session.user) {
			// i.e. user not logged in
			redirect(controller:'login',action:'login')
			return false
		}
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[offerInstanceList: Offer.list(params), offerInstanceTotal: Offer.count()]
	}

	def create() {
		if(!session.user) {
			// i.e. user not logged in
			redirect(controller:'login',action:'login')
			return false
		}
		[offerInstance: new Offer(params)]
	}

	def save() {
		def offerInstance = new Offer(params)
		if (!offerInstance.save(flush: true)) {
			render(view: "create", model: [offerInstance: offerInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'offer.label', default: 'Offer'),
			offerInstance.id
		])
		redirect(action: "show", id: offerInstance.id)
	}

	def show() {
		if(!session.user) {
			// i.e. user not logged in
			redirect(controller:'login',action:'login')
			return false
		}
		def offerInstance = Offer.get(params.id)
		if (!offerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'offer.label', default: 'Offer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[offerInstance: offerInstance]
	}

	def edit() {
		if(!session.user) {
			// i.e. user not logged in
			redirect(controller:'login',action:'login')
			return false
		}
		def offerInstance = Offer.get(params.id)
		if (!offerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'offer.label', default: 'Offer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[offerInstance: offerInstance]
	}

	def update() {
		def offerInstance = Offer.get(params.id)
		if (!offerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'offer.label', default: 'Offer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (offerInstance.version > version) {
				offerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'offer.label', default: 'Offer')]
						as Object[],
						"Another user has updated this Offer while you were editing")
				render(view: "edit", model: [offerInstance: offerInstance])
				return
			}
		}

		offerInstance.properties = params

		if (!offerInstance.save(flush: true)) {
			render(view: "edit", model: [offerInstance: offerInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'offer.label', default: 'Offer'),
			offerInstance.id
		])
		redirect(action: "show", id: offerInstance.id)
	}

	def delete() {
		def offerInstance = Offer.get(params.id)
		if (!offerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'offer.label', default: 'Offer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		try {
			offerInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'offer.label', default: 'Offer'),
				params.id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'offer.label', default: 'Offer'),
				params.id
			])
			redirect(action: "show", id: params.id)
		}
	}
}
